package com.czbgradefive.shop.service.impl;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.czbgradefive.common.core.utils.DateUtils;
import com.czbgradefive.common.security.utils.SecurityUtils;
import com.czbgradefive.shop.domain.MallProductRuleAttrInfo;
import com.czbgradefive.shop.domain.model.MallProductAttrInfoModel;
import com.czbgradefive.shop.service.IMallProductRuleAttrInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.czbgradefive.shop.mapper.MallProductRuleInfoMapper;
import com.czbgradefive.shop.domain.MallProductRuleInfo;
import com.czbgradefive.shop.service.IMallProductRuleInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品规格Service业务层处理
 * 
 * @author czbgradefive
 * @date 2024-05-08
 */
@Service
public class MallProductRuleInfoServiceImpl implements IMallProductRuleInfoService 
{
    @Autowired
    private MallProductRuleInfoMapper mallProductRuleInfoMapper;
    @Autowired
    private IMallProductRuleAttrInfoService iMallProductRuleAttrInfoService;

    public static final String UPDATE_FLAG="update";
    public static final String INSERT_FLAG="insert";
    /**
     * 查询商品规格
     * 
     * @param id 商品规格主键
     * @return 商品规格
     */
    @Override
    public MallProductRuleInfo selectMallProductRuleInfoById(Long id)
    {
        return mallProductRuleInfoMapper.selectMallProductRuleInfoById(id);
    }

    /**
     * 查询商品规格列表
     * 
     * @param mallProductRuleInfo 商品规格
     * @return 商品规格
     */
    @Override
    public List<MallProductRuleInfo> selectMallProductRuleInfoList(MallProductRuleInfo mallProductRuleInfo)
    {
        return mallProductRuleInfoMapper.selectMallProductRuleInfoList(mallProductRuleInfo);
    }

    /**
     * 新增商品规格
     * 
     * @param mallProductRuleInfo 商品规格
     * @return 结果
     */

    @Transactional
    @Override
    public int insertMallProductRuleInfo(MallProductRuleInfo mallProductRuleInfo)
    {

        Long userid = SecurityUtils.getLoginUser().getUserid();
//        Date nowDate = DateUtils.getNowDate();
//
//        genTableColumn.setCreateBy(String.valueOf(userid));
//        genTableColumn.setUpdateBy(String.valueOf(userid));
        mallProductRuleInfo.setCreateBy(String.valueOf(userid));
        mallProductRuleInfo.setUpdateBy(String.valueOf(userid));
        mallProductRuleInfo.setCreateTime(DateUtils.getNowDate());
        mallProductRuleInfo.setUpdateTime(DateUtils.getNowDate());
        int i = mallProductRuleInfoMapper.insertMallProductRuleInfo(mallProductRuleInfo);
        extracted(mallProductRuleInfo, i,INSERT_FLAG);

        return i;
    }

    /**
     * 修改商品规格
     * 
     * @param mallProductRuleInfo 商品规格
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMallProductRuleInfo(MallProductRuleInfo mallProductRuleInfo)
    {
        mallProductRuleInfo.setUpdateTime(DateUtils.getNowDate());
        Long userid = SecurityUtils.getLoginUser().getUserid();
        mallProductRuleInfo.setUpdateBy(String.valueOf(userid));
        int i = mallProductRuleInfoMapper.updateMallProductRuleInfo(mallProductRuleInfo);
        extracted(mallProductRuleInfo, i,UPDATE_FLAG);
        return i;
    }

    private void extracted(MallProductRuleInfo mallProductRuleInfo, int i,String flag) {
        if (i >0){
            Long ruleId = mallProductRuleInfo.getId();
            ArrayList<MallProductRuleAttrInfo> ruleAttrInfoList = new ArrayList<>();
            String ruleAttr = mallProductRuleInfo.getRuleAttr();
            List<MallProductAttrInfoModel> mallProductAttrInfoModels = JSON.parseArray(ruleAttr, MallProductAttrInfoModel.class);

            if (CollectionUtils.isNotEmpty(mallProductAttrInfoModels)){
                for (MallProductAttrInfoModel mallProductAttrInfoModel : mallProductAttrInfoModels) {
                    List<String>ruleAttrList=mallProductAttrInfoModel.getRuleAttrList();
                    for (String ruleAttrString : ruleAttrList) {
                        MallProductRuleAttrInfo mallProductRuleAttrInfo = new MallProductRuleAttrInfo();
                        mallProductRuleAttrInfo.setAttrValue(ruleAttrString);
                        mallProductRuleAttrInfo.setRuleId(ruleId);

                        ruleAttrInfoList.add(mallProductRuleAttrInfo);
//                        mallProductRuleInfo.setCreateBy(String.valueOf(userid));
//                        mallProductRuleInfo.setUpdateBy(String.valueOf(userid));
//                        mallProductRuleInfo.setCreateTime(DateUtils.getNowDate());
//                        mallProductRuleInfo.setUpdateTime(DateUtils.getNowDate());
                    }
                }
                if (UPDATE_FLAG.equals(flag)){
                    iMallProductRuleAttrInfoService.deleteMallproductRuleAttrInfoByRuleId(ruleId);
                }
                iMallProductRuleAttrInfoService.insertMallProductRuleAttrInfoList(ruleAttrInfoList);
            }
        }
    }

    /**
     * 批量删除商品规格
     * 
     * @param ids 需要删除的商品规格主键
     * @return 结果
     */
    @Override
    public int deleteMallProductRuleInfoByIds(Long[] ids)
    {
        int i = mallProductRuleInfoMapper.deleteMallProductRuleInfoByIds(ids);
        for (Long id : ids) {
            iMallProductRuleAttrInfoService.deleteMallproductRuleAttrInfoByRuleId(id);
        }

        return i;
    }

    /**
     * 删除商品规格信息
     * 
     * @param id 商品规格主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMallProductRuleInfoById(Long id)
    {
        return mallProductRuleInfoMapper.deleteMallProductRuleInfoById(id);
    }


    public static void main(String[] args) {
        String str="[{\"ruleType\":\"测试\",\"ruleAttrListButtonFlag\":true,\"ruleAttrListFormFlag\":false,\"ruleAttrList\":[\"测试1\",\"测试2\"],\"ruleAttrName\":\"\"},{\"ruleType\":\"测试1\",\"ruleAttrListButtonFlag\":true,\"ruleAttrListFormFlag\":false,\"ruleAttrList\":[\"测试\"],\"ruleAttrName\":\"\"}]";
        List<MallProductAttrInfoModel> mallProductAttrInfoModels = JSON.parseArray(str, MallProductAttrInfoModel.class);
        for (MallProductAttrInfoModel mallProductAttrInfoModel : mallProductAttrInfoModels) {
            System.out.println(mallProductAttrInfoModel);
        }
    }

}
